package com.weiyi.mashangzhu.Landlord.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.okhttp.Request;
import com.weiyi.mashangzhu.API.API;
import com.weiyi.mashangzhu.Application.MyApp;
import com.weiyi.mashangzhu.Base.BaseActivity;
import com.weiyi.mashangzhu.R;
import com.weiyi.mashangzhu.Utils.LoadingDialog;
import com.weiyi.mashangzhu.Utils.SPUtils;
import com.weiyi.mashangzhu.Utils.T;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class SetPriceActivity extends BaseActivity {
    boolean flag = false;
    @BindView(R.id.button)
    ImageView button;
    @BindView(R.id.yajin)
    TextView yajin;
    @BindView(R.id.tv_price_day)
    TextView tvPriceDay;
    EditText price;
    String info,id;
    String addperson;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_price);
        ButterKnife.bind(this);
        Intent intent=getIntent();
        info=intent.getStringExtra("info");
        id=intent.getStringExtra("id");

        if (info.equals("post")){
            if (MyApp.price_price != null) {
                tvPriceDay.setText(MyApp.price_price + "元/每天");
            }
            if (MyApp.price_yajian != null) {
                yajin.setText(MyApp.price_yajian + "元");
            }
            Log.e("ooooooooooo","=="+MyApp.price_add);
            if (MyApp.price_add != null) {

                if (MyApp.price_add.equals("0")) {
                    button.setImageResource(R.mipmap.button21);
                } else {
                    button.setImageResource(R.mipmap.button);
                }

            }
        }else {
            MyApp.price_add="0";
            //修改房源
            LoadingDialog.show(this);
            OkHttpUtils.get()
                    .addParams("token", SPUtils.get(this,"token","")+"")
                    .addParams("id",id)
                    .url(API.BaseApi_L+API.GetHousePrice)
                    .build()
                    .execute(new StringCallback() {
                        @Override
                        public void onError(Request request, Exception e) {
                            T.showShort(SetPriceActivity.this,"网络连接失败");
                            LoadingDialog.dismiss(SetPriceActivity.this);
                        }

                        @Override
                        public void onResponse(String response) {
                            Log.e("pppppppp","re="+response);
                            LoadingDialog.dismiss(SetPriceActivity.this);
                            try {
                                JSONObject jsonObject=new JSONObject(response);
                                if (jsonObject.getInt("status")==0){
                                    JSONArray jsonArray=jsonObject.getJSONArray("data");
                                    JSONObject jsonObject1=jsonArray.getJSONObject(0);
                                    tvPriceDay.setText(jsonObject1.getString("hPrice") + "元/每天");
                                    yajin.setText(jsonObject1.getString("hdeposit")+ "元");
                                    addperson=jsonObject1.getString("hAddpeople");
                                    if (jsonObject1.getString("hAddpeople").equals("1")){
                                        button.setImageResource(R.mipmap.button);
                                    }else {
                                        button.setImageResource(R.mipmap.button21);
                                    }
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }
                    });
        }

    }

    private void price(final String info) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        View view = View.inflate(this, R.layout.dialog_price_set, null);
        final TextView baocun = (TextView) view.findViewById(R.id.baocun);
        price = (EditText) view.findViewById(R.id.price);
        builder.setView(view);
        final AlertDialog dialog = builder.show();
        baocun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                if (info.equals("日常价")) {
                    tvPriceDay.setText(price.getText() + "元/每天");
                    MyApp.price_price = price.getText() + "";
                } else if (info.equals("押金")) {
                    yajin.setText(price.getText() + "元");
                    MyApp.price_yajian = price.getText() + "";
                }


            }
        });
    }

    @OnClick({R.id.back, R.id.baocun, R.id.price_day, R.id.price_yajin, R.id.button})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.baocun:
                if (info.equals("post")){
                    //上传房源
                    Intent intent = new Intent();
                    if (MyApp.price_yajian != null && MyApp.price_price != null) {
                        intent.putExtra("info", "完成");
                    } else {
                        intent.putExtra("info", "未完成");
                    }
                    intent.putExtra("price_price", MyApp.price_price);
                    intent.putExtra("price_yajian", MyApp.price_yajian);
                    intent.putExtra("price_add", MyApp.price_add);
                    setResult(0x004, intent);
                    finish();
                }else {
                    //修改房源
                    Log.e("ppppppp",(tvPriceDay.getText()+"").substring(0,(tvPriceDay.getText()+"").indexOf("元")));
                    Log.e("ppppppp",(yajin.getText()+"").substring(0,(yajin.getText()+"").indexOf("元")));
                    Log.e("ppppppp",addperson);
                    LoadingDialog.show(this);
                    OkHttpUtils.get()
                            .addParams("token",SPUtils.get(this,"token","")+"")
                            .addParams("id",id)
                            .addParams("hPrice",(tvPriceDay.getText()+"").substring(0,(tvPriceDay.getText()+"").indexOf("元")))
                            .addParams("hdeposit",(yajin.getText()+"").substring(0,(yajin.getText()+"").indexOf("元")))
                            .addParams("hAddpeople",addperson)
                            .url(API.BaseApi_L+API.UpdateHousePrice)
                            .build()
                            .execute(new StringCallback() {
                                @Override
                                public void onError(Request request, Exception e) {
                                    T.showShort(SetPriceActivity.this,"网络连接失败");
                                    LoadingDialog.dismiss(SetPriceActivity.this);

                                }

                                @Override
                                public void onResponse(String response) {
                                    LoadingDialog.dismiss(SetPriceActivity.this);
                                    try {
                                        JSONObject jsonObject=new JSONObject(response);
                                        if (jsonObject.getInt("status")==0){
                                            T.showShort(SetPriceActivity.this,jsonObject.getString("msg") + " 后台审核中...");
                                            finish();
                                        } else {
                                            T.showShort(SetPriceActivity.this,jsonObject.getString("msg"));
                                        }
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }
                                }
                            });


                }

                break;
            case R.id.price_day:
                price("日常价");
                break;
            case R.id.price_yajin:
                price("押金");
                break;
            case R.id.button:
                if (info.equals("post")){
                    if (flag == false) {
                        button.setImageResource(R.mipmap.button);
                        MyApp.price_add = "1";
                        flag = true;
                    } else {
                        flag = false;
                        button.setImageResource(R.mipmap.button21);
                        MyApp.price_add = "0";
                    }
                }else {
                    if (flag == false) {
                        button.setImageResource(R.mipmap.button21);
                        addperson="0";
                        flag = true;
                    } else {
                        button.setImageResource(R.mipmap.button);
                        addperson="1";
                        flag = false;
                    }
                }
                Log.e("ooooooooooo","==2"+MyApp.price_add);

                break;
        }
    }
}
